﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Rj_GsInsurance.Client.DependencyModel;
using Rj_GsInsurance.Client.Pages;
using Rj_GsInsurance.Client.Services;
using Rj_GsInsurance.Client.Services.Contracts;
using Rj_GsInsurance.Client.ViewModels;
using Rj_GsInsurance.Server.DataBaseContext;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Threading;
using Wpf.Ui;
using Rj_GsInsurance.Server.AutoMapper;
using Rj_GsInsurance.Server.Services;
using Rj_GsInsurance.Server.Services.Impl;
using Rj_GsInsurance.Client.AutoMapper;

namespace Rj_GsInsurance.Client
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        private static readonly IHost _host = Host.CreateDefaultBuilder()
        .ConfigureAppConfiguration(c =>
        {
            c.SetBasePath(AppContext.BaseDirectory);
            c.AddJsonFile("ConfigFile/SystemConfig.json");
        })
        .ConfigureServices(
            (builder, services) =>
            {
                // App Host
                services.AddHostedService<ApplicationHostService>();
                services.AddDbContext<HisDbContext>(option => { option.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); });
                // Top-level pages
                services.AddSingleton<AllControlsViewModel>();
                services.AddSingleton<ReconciliationViewModel>();
                services.AddSingleton<DashboardPage>();
                services.AddSingleton<SettingsPage>();
                services.AddSingleton<DevUploadPage>();
                services.AddSingleton<DocUploadPage>();
                services.AddSingleton<ReconciliationPage>();
                // Main window container with navigation
                services.AddSingleton<ISnackbarService, SnackbarService>();
                services.AddSingleton<IContentDialogService, ContentDialogService>();
                services.AddSingleton<IGsInsuranceServer, GsInsuranceServer>();
                services.AddSingleton<IWindow, MainWindow>();
                services.AddSingleton<MainWindowViewModel>();
                services.AddSingleton<INavigationService, NavigationService>();
                services.AddSingleton<ISnackbarService, SnackbarService>();
                services.AddSingleton<IContentDialogService, ContentDialogService>();
                services.AddSingleton<WindowsProviderService>();

                services.AddAutoMapper(typeof(ServiceProfile));
                services.AddAutoMapper(typeof(AppProfile));

                // All other pages and view models
                services.AddTransientFromNamespace("Rj_GsInsurance.Client.Pages", ClientAssembly.Asssembly);
                services.AddTransientFromNamespace("Rj_GsInsurance.Client.ViewModels", ClientAssembly.Asssembly);
            }
        )
        .Build();

        /// <summary>
        /// Gets registered service.
        /// </summary>
        /// <typeparam name="T">Type of the service to get.</typeparam>
        /// <returns>Instance of the service or <see langword="null"/>.</returns>
        public static T GetRequiredService<T>()
            where T : class
        {
            return _host.Services.GetRequiredService<T>();
        }

        /// <summary>
        /// Occurs when the application is loading.
        /// </summary>
        private void OnStartup(object sender, StartupEventArgs e)
        {
            _host.Start();
        }

        /// <summary>
        /// Occurs when the application is closing.
        /// </summary>
        private void OnExit(object sender, ExitEventArgs e)
        {
            _host.StopAsync().Wait();

            _host.Dispose();
        }

        /// <summary>
        /// Occurs when an exception is thrown by an application but not handled.
        /// </summary>
        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
        }
    }

}
